const verifyServerMV = require('express').Router()

verifyServerMV.route('/wx')
    .get((req, res) => {
        const { token } = require('../config')
        const data = req.query
        //如果收到的数据没有echostr字段就直接返回error
        if (!data.echostr) {
            res.writeHead(403, { 'content-type': 'text/html' })
            res.end(`<div style="
    display: flex; 
    justify-content: center;
    align-items: center;
    flex-direction: column;
    height: 100vh;
    ">
    <h1>UNAUTHORIZED ACCESS!!!</h1>

    <a href="https://ivanccc.com">
        <h4>back to my homepage<h4><a>
                    <p>Created By: ivan chen</p>
</div>`)
        }
        else {
            //把token保存在data中传入验证函数
            data.token = token
            const status = require('../tools/verifyServer')(data)
            //验证正确才返回echostr
            if (status) res.end(data.echostr)
            else {
                res.writeHead(403, { 'content-type': 'text/html' })
                res.end(`<div style="
    display: flex; 
    justify-content: center;
    align-items: center;
    flex-direction: column;
    height: 100vh;
    ">
    <h1>UNAUTHORIZED ACCESS!!!</h1>

    <a href="https://ivanccc.com">
        <h4>back to my homepage<h4><a>
                    <p>Created By: ivan chen</p>
</div>`)
            }
        }
    })

module.exports = verifyServerMV




